package com.bdilab.iot.controller;

import com.bdilab.iot.common.ResultMap;
import com.bdilab.iot.consts.Constants;
import com.bdilab.iot.model.User;
import com.bdilab.iot.service.UserService;
import com.bdilab.iot.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author WangNing
 * @version 1.0
 * @date 2020/8/1 10:52
 * 测试用户接口
 */
@RestController
@Api(tags = "测试接口")
@RequestMapping(value = Constants.BASE_API_PATH + "/login")
public class LoginController {


    @Autowired
    UserService userService;

    @Autowired
    TokenUtils tokenUtils;

    @ApiOperation("输入用户名生成测试所需Token")
    @GetMapping("/gen_token_for_test")
    public ResponseEntity genToken(@RequestParam(value = "userName") String userName, HttpServletRequest request){
        User user = userService.getUserByUserName(userName);
        String tokenString = Constants.TOKEN_PREFIX + " "+ tokenUtils.generateToken(user);
        ResultMap resultMap = new ResultMap().success(tokenUtils.generateToken(user)).payload(tokenString);
        return ResponseEntity.ok(resultMap);
    }


}
